<?php

class Control_Menu_Admintop extends QUI_Control_Abstract
{
    function render()
    {
    	
    	$ctx = QContext::instance();
    	if($ctx['action'] == 'login') return '';
    	$udi = $ctx->requestOriUDI();
    	$app = MyApp::instance();
        
        // get all menus
    	$menus = Q::ini('appini/adminMenu');
    	
    	// get menus that current user can access
    	foreach ($menus as $menuText => & $menu){
    		if(isset($menu['udi']) && !$app->authorizedUDI($app->currentUserRoles(),$menu['udi'])){
    			unset($menus[$menuText]);
    			continue;
    			
    		}elseif (isset($menu['submenu'])) {
    			foreach ($menu['submenu'] as $submenuText => $submenu) {
    				if (isset($submenu['udi']) && !$app->authorizedUDI($app->currentUserRoles(),$submenu['udi'])){
    					unset($menu['submenu'][$submenuText]);
    					continue;
    				}
                    // current menu box
		    		if($ctx->normalizeUDI($submenu['udi']) == $udi){
		    			$app->currentMenu = array($menuText => $menu);
		    		}
    			}
    		}

    		// current menu box
    		if(isset($menu['udi']) && $ctx->normalizeUDI($menu['udi']) == $udi){
    			$app->currentMenu = array($menuText => $menu);
    		}
    	}
        
      // setup logout menu
      $currentUser = $app->currentUser();
      if (!empty($currentUser)){
	    	$menus['退出系统'] = array(
					'udi'=>'user/logout'
				);
      }
        
      $this->_view['menus'] = $menus;
        
        
        // 渲染视图并返回结果
      return $this->_fetchView(dirname(__FILE__) . '/admintop_view');
    }
}